const Koa=require('koa');//导入koa包
const { bodyParser } = require('@koa/bodyparser');//导入bodypaerser
const Router=require('@koa/router');//导入路由包
const app=new Koa();//实例化应用对象
const router=new Router();//实例化路由，总路由
// const userRouter=require('./router/user')//子路由，用户路由
// const wenzhangRouter=require('./router/wenzhang')//子路由，文章路由
// const jingdianRouter=require('./router/jingdian')//子路由，景点路由
const ticketRouter=require('./router/ticket')//子路由，门票路由

const serve=require('koa-static')
const cors=require('@koa/cors')//跨域

const { sequelize, TicketModel } = require('./database/index'); // 导入 sequelize 实例和 TicketModel
const ticket = require('./database/model/ticket');

app.use(bodyParser());//针对post请求

(async function(){
    sequelize.sync({force:false})
    console.log("数据库已同步");
    //插入门票
    await TicketModel.create({ // 使用 TicketModel 创建记录
        ticket_name: '蔡徐坤打篮球',
        ticket_price: '123456',
        ticket_num: 5,
    });
    console.log("门票已插入");
    })();

app.use(cors({
    origin(ctx){
        return '*';
    }
}));

app.use(serve(__dirname+'/images'));

//配置路由信息-子路由
router.use("/ticket",ticketRouter.routes(),ticketRouter.allowedMethods())

//配置总路由
app.use(router.routes()).use(router.allowedMethods());

//添加中间件，中间件就是函数
app.use(async ctx=>{
    // let request=ctx.query;
    const result=ctx.request.body;
    console.log(request)
    // ctx.body='牢大肘击';
});
//监听端口在3000端口
app.listen(3000);